package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    //查询
    @GetMapping
    public Result clazzSelect(ClazzQueryParam clazzQueryParam){
        log.info("分页查询班级信息");

        PageResult<Clazz> pageResult = clazzService.clazzSelect(clazzQueryParam);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result clazzDelete(@PathVariable Integer id){
        log.info("根据id删除班级信息"+id);
        Result result = clazzService.clazzDelete(id);
        return result;
    }

    @PostMapping
    public Result clazzInsert(@RequestBody Clazz clazz){
        log.info("添加班级信息");
        clazzService.clazzInsert(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result clazzGetById(@PathVariable Integer id){
        log.info("根据id查询班级信息");
        Clazz clazz =  clazzService.clazzGetById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result clazzUpdate(@RequestBody Clazz clazz){
        log.info("修改班级信息");
        clazzService.clazzUpdate(clazz);
        return Result.success();
    }

    @GetMapping("list")
    public Result findAll(){
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }
}
